import subprocess
import os
from PIL import Image
import numpy as np

class EmojiAnimator:
    def __init__(self, input_path, output_path):
        self.input_path = input_path
        self.output_path = output_path

    def create_bounce_effect(self):
        """创建弹跳效果的GIF动画"""
        try:
            # 读取原始图片
            img = Image.open(self.input_path)
            frames = []
            
            # 创建10帧的弹跳动画
            for i in range(10):
                # 计算y轴偏移
                offset = int(np.sin(i * np.pi / 5) * 20)
                
                # 创建新的画布
                canvas = Image.new('RGBA', (img.width, img.height + 40), (0, 0, 0, 0))
                # 粘贴原图到新位置
                canvas.paste(img, (0, 20 + offset))
                frames.append(canvas)

            # 保存为GIF
            frames[0].save(
                self.output_path,
                save_all=True,
                append_images=frames[1:],
                duration=50,  # 每帧持续50ms
                loop=0
            )
            return True
        except Exception as e:
            print(f"创建弹跳效果失败: {str(e)}")
            return False

    def create_shake_effect(self):
        """创建抖动效果的GIF动画"""
        try:
            img = Image.open(self.input_path)
            frames = []
            
            # 创建8帧的抖动动画
            for i in range(8):
                # 随机偏移
                offset_x = np.random.randint(-5, 6)
                offset_y = np.random.randint(-5, 6)
                
                canvas = Image.new('RGBA', (img.width + 10, img.height + 10), (0, 0, 0, 0))
                canvas.paste(img, (5 + offset_x, 5 + offset_y))
                frames.append(canvas)

            frames[0].save(
                self.output_path,
                save_all=True,
                append_images=frames[1:],
                duration=50,
                loop=0
            )
            return True
        except Exception as e:
            print(f"创建抖动效果失败: {str(e)}")
            return False

    def create_rotate_effect(self):
        """创建旋转效果的GIF动画"""
        try:
            img = Image.open(self.input_path)
            frames = []
            
            # 创建12帧的旋转动画
            for i in range(12):
                # 旋转角度
                angle = i * 30
                
                # 旋转图片
                rotated = img.rotate(angle, expand=True)
                
                # 创建新的画布
                canvas = Image.new('RGBA', rotated.size, (0, 0, 0, 0))
                canvas.paste(rotated, (0, 0))
                frames.append(canvas)

            frames[0].save(
                self.output_path,
                save_all=True,
                append_images=frames[1:],
                duration=50,
                loop=0
            )
            return True
        except Exception as e:
            print(f"创建旋转效果失败: {str(e)}")
            return False